import React, { useState } from 'react';
import { Form, Input, Button, message } from 'antd';
import axios from 'axios';

const Auth = () => {
    const [isLogin, setIsLogin] = useState(true);

    const handleSubmit = async (values) => {
        const url = isLogin ? '/api/auth/login/' : '/api/auth/register/';
        try {
            const response = await axios.post(url, values);
            message.success('操作成功');
            // 设定用户信息
        } catch (error) {
            message.error('操作失败');
        }
    };

    return (
        <Form onFinish={handleSubmit}>
            <Form.Item name="username" rules={[{ required: true, message: '请输入用户名' }]}>
                <Input placeholder="用户名" />
            </Form.Item>
            <Form.Item name="password" rules={[{ required: true, message: '请输入密码' }]}>
                <Input.Password placeholder="密码" />
            </Form.Item>
            <Form.Item>
                <Button type="primary" htmlType="submit">{isLogin ? '登录' : '注册'}</Button>
                <Button type="link" onClick={() => setIsLogin(!isLogin)}>
                    {isLogin ? '没有账户? 立即注册' : '已有账户? 立即登录'}
                </Button>
            </Form.Item>
        </Form>
    );
};

export default Auth;
